class award extends egret.DisplayObjectContainer {

        /****************************** 资源与loading配置 ***************************/

        static resUrl: string   = "resource/default.res.json";
        static resRoot: string  = "resource/";
        static resGroup: string = "lucky";
        static loading: any     = LoadingUI;


        public constructor() {
                super();
                this.addEventListener(egret.Event.ADDED_TO_STAGE, this.createGameScene, this);
        }

        private createGameScene() {
                //创建背景图片
                var indexBg = creatImg("blue_bg_png");
                indexBg.width = 640;
                indexBg.height = 1136;
                this.addChild(indexBg);
                //创建背景图片
                var shuiwen = creatImg("shuiwen_png");
                shuiwen.width = 640;
                shuiwen.height = 1136;
                shuiwen.scaleX = 1.01;
                shuiwen.scaleY = 1.1;
                shuiwen.anchorOffsetX = this.stage.$stageWidth * .5;
                shuiwen.anchorOffsetY = this.stage.$stageHeight * .5;
                shuiwen.x = shuiwen.width*.5;
                shuiwen.y = shuiwen.height*.5;
                this.addChild(shuiwen);

                var move = egret.Tween.get(shuiwen, { loop: true });
               move.to({ x: shuiwen.width * .5 + 5,y: shuiwen.height * .5 + 60}, 7000).wait(500).to({ x: shuiwen.width * .5,y: shuiwen.height * .5 }, 7000);
                //创建logo图片
                var logoImg1 = creatImg("logo_03_png");
                logoImg1.x = (this.$stage.stageWidth - logoImg1.width) * 0.5;
                logoImg1.y = this.$stage.stageHeight - 120;
                this.addChild(logoImg1);
                //奖牌（存放图片）
                var awardImg = creatImg("rank_bg_png");
                awardImg.x = 60;
                this.addChild(awardImg);
                //奖牌（初始默认是“？”图片）
                var awardOffset = creatImg("wenhao_png");
                awardOffset.x = 130;
                awardOffset.y = 190;
                this.addChild(awardOffset);
                //显示奖品的名字信息
                var awardName = new egret.TextField();
                awardName.text = "? ? ? ? ? ?";
                awardName.size = 40;
                awardName.x = 300;
                awardName.y = 260;
                this.addChild(awardName);
                //创建抽奖按钮
                var start = creatText("点击抽奖", 60, 0xfe3c63, "start_png", 140, 180);
                this.addChild(start);
                start.$children[1].x = 20;
                start.x = 230;
                start.y = 603;
                var awardInfo = [
                        { "x": 135, "y": 510, "text": "奥地利客户外包", "picture": "backpack_03_png", "fontSize": "20", "textColor": 0x000000 },
                        { "x": 135, "y": (510 + 190), "text": "小米运动手环", "picture": "shouhuan_03_png", "fontSize": "20", "textColor": 0x000000 },
                        { "x": 135, "y": (510 + 2 * 190), "text": "安联运动意外保障计划（7天）", "picture": "yaoxiang_03_png", "fontSize": "20", "textColor": 0x000000 },
                        { "x": (135 + 190), "y": (510 + 2 * 190), "text": "多功能运动腰包", "picture": "yaobao_03_png", "fontSize": "20", "textColor": 0x000000 },
                        { "x": (135 + 2 * 190), "y": (510 + 2 * 190), "text": "迪卡侬运动毛巾", "picture": "maojin_03_png", "fontSize": "20", "textColor": 0x000000 },
                        { "x": (135 + 2 * 190), "y": (510 + 190), "text": "谢谢参与", "picture": "", "fontSize": "63", "textColor": 0x999999 },
                        { "x": (135 + 2 * 190), "y": 510, "text": "乐扣运动水壶", "picture": "water_03_png", "fontSize": "20", "textColor": 0x000000 },
                        { "x": (135 + 190), "y": 510, "text": "迪卡侬透气T桖", "picture": "Tshirt_03_png", "fontSize": "20", "textColor": 0x000000 },
                        { "x": -3000, "y": -3000, "text": "", "picture": "", "fontSize": "", "textColor": 0x000000 }
                ]
                var lucky = new egret.Sprite();
                this.addChild(lucky);
                //循环数组实现九宫格布局
                for (var i: number = 0; i < awardInfo.length; i++) {
                        var jiugong = new egret.Sprite();
                        jiugong = creatShape(awardInfo[i].x, awardInfo[i].y, 170, 170, awardInfo[i].text, awardInfo[i].picture, awardInfo[i].fontSize, awardInfo[i].textColor);
                        jiugong.anchorOffsetX = jiugong.width / 2;
                        jiugong.anchorOffsetY = jiugong.height / 2;
                        jiugong.graphics.endFill();
                        lucky.addChild(jiugong);
                }
                // console.log(jiugong);
                //给开始抽奖添加点击事件
                start.touchEnabled = true;
                start.addEventListener(egret.TouchEvent.TOUCH_BEGIN, function () {
                        egret.Tween.get(start).to({ alpha: .8 }, 100);
                        $.ajax({
                                type: "GET", //请求的方式
                                url: "http://student.bluej.cn/index/index/lucky_draw1", //请求的路径地址
                                async: true, //是否异步
                                dataType: "jsonp", //dataType设置为jsonp可以解决跨域问题
                                jsonp: "callback", //传递给请求处理程序或页面的，用以获得jsonp回调函数名的参数名(一般默认为:callback)
                                jsonpCallback: "lucky_num", //自定义的jsonp回调函数名称，默认为jQuery自动生成的随机函数名，也可以写"?"，jQuery会自动为你处理数据
                                success: (res) => { //请求成功并返回数据的回调
                                        //后面任何基于res数据的代码都要写在success的回调函数里面
                                        random = res[0].num;
                                        console.log(random);
                                        change();      //抽奖九宫格转动函数回调
                                },
                                error: (a, b, c) => {
                                        console.log(a); //错误对象
                                        //创建信息弹出层
                                        let dialogs: egret.DisplayObjectContainer = Dialogs.message([
                                                {
                                                        title: "Warning",
                                                        content: "网络环境差，请重新游戏！"
                                                }
                                        ]);
                                        //打开信息弹出层
                                        Effect.fadeIn(dialogs, this.stage);
                                        console.log(b); //错误类型
                                        console.log(c); //错误的具体信息
                                }
                        });
                }, this);
                start.addEventListener(egret.TouchEvent.TOUCH_END, function () {
                        egret.Tween.get(start).to({ alpha: 1 }, 100);
                }, this)
                lucky.$children[5].$children[1].y = -22;
                var startSound = RES.getRes("di_mp3");	//加载抽奖音效
                var endSound = RES.getRes("end_wav");
                var endSoundFalse = RES.getRes("false_wav");
                // var endSound = RES.getRes("choujiangjieshu_mp3");	//加载抽奖结束音效
                /*****************抽奖转动逻辑部分*****************/
                var index = 0,              //奖品下标
                        self = this,        //this指向
                        timer,              //定时器
                        speed = 1000,       //转的速度
                        random,             //随机数
                        times = 1;          //转的圈数 

                /*************"再抽一次",抽奖结束后再加入舞台*************/
                var onceAginBtn = creatText("再抽一次", 60, 0xfe3c63, "start_png", 140, 180);
                onceAginBtn.$children[1].x = 20;
                onceAginBtn.x = 230;
                onceAginBtn.y = 603;
                //分享弹出层
                onceAginBtn.touchEnabled = true;
                onceAginBtn.addEventListener(egret.TouchEvent.TOUCH_BEGIN, function () {
                        //创建分享弹出层
                        let shape: egret.DisplayObjectContainer = Dialogs.share("分享一下\n再次获取抽奖机会~");
                        //打开分享弹出层
                        Effect.fadeIn(shape, this.stage);
                }, this)

                /***************抽奖主逻辑函数********************/
                function change() {
                        times < 5 ? speed -= 100 : speed += 100;   //速度先慢后快最后减速
                        speed = Math.max(speed, 200);              //速度最快100

                        if (index >= 7) {
                                index = 0;
                                times++;
                        } else {
                                index++;
                        }
                        egret.Tween.get(lucky.$children[index]).to({ scaleX: 1.1, scaleY: 1.1 }, 100).call(() => {
                                egret.Tween.get(lucky.$children[index]).to({ scaleX: 1, scaleY: 1 }, 100)
                        });

                        //选中时背景变化
                        lucky.removeChild(jiugong);
                        jiugong = creatShape(awardInfo[index].x, awardInfo[index].y, 187, 187, awardInfo[index].text, awardInfo[index].picture, awardInfo[index].fontSize, awardInfo[index].textColor, 0xfff530, 10, 0xff9f34);
                        jiugong.anchorOffsetX = jiugong.width * .5;
                        jiugong.anchorOffsetY = jiugong.height * .5;
                        jiugong.graphics.endFill();
                        jiugong.getChildAt(2).x = 94;
                        jiugong.getChildAt(1).y = -30;
                        lucky.addChild(jiugong);
                        //音效
                        startSound.play(0, 1);
                        //显示在奖牌内的奖品
                        self.removeChild(awardOffset);
                        awardOffset = creatImg(awardInfo[index].picture);
                        awardOffset.x = 165;
                        awardOffset.y = 260;
                        awardOffset.anchorOffsetX = awardOffset.width * 0.5;
                        awardOffset.anchorOffsetY = awardOffset.height * 0.5;
                        self.addChild(awardOffset);
                        //显示奖品的描述信息
                        self.removeChild(awardName);
                        awardName.text = awardInfo[index].text;
                        awardName.width = 300;
                        awardName.height = 100;
                        awardName.size = 40;
                        awardName.x = 300;
                        awardName.y = 220;
                        self.addChild(awardName);

                        /***************抽奖结束条件*****************/
                        if (times == 6 && index == random) {
                                clearTimeout(timer);

                                //点击分享，再抽一次
                                self.removeChild(start);
                                self.addChildAt(onceAginBtn, 2);
                                //显示奖品的描述信息
                                self.removeChild(awardName);
                                awardName.text = "恭喜你！";
                                awardName.size = 40;
                                awardName.x = 300;
                                awardName.y = 180;
                                self.addChild(awardName);

                                var awardName1 = new egret.TextField();
                                awardName1.text = "你的奖品为";
                                awardName1.size = 40;
                                awardName1.x = 300;
                                awardName1.y = 225;
                                self.addChild(awardName1);

                                var awardName2 = new egret.TextField();
                                awardName2.text = awardInfo[index].text;
                                awardName2.width = 300;
                                awardName2.height = 80;
                                awardName2.size = 40;
                                awardName2.x = 300;
                                awardName2.y = 275;
                                self.addChild(awardName2);
                                if (awardName2.text == "谢谢参与") {
                                        awardName.text = "很遗憾！";
                                        awardName1.text = "谢谢参与";
                                        awardName2.text = "您可以再抽一次";
                                        //未中奖音效
                                        endSoundFalse.play(0, 1);
                                } else {
                                        //中奖音效
                                        endSound.play(0, 1);
                                }

                        } else {
                                timer = setTimeout(change, speed);
                                //点击抽奖，切换到抽奖中...
                                self.removeChild(start);
                                start = creatText("抽奖中...", 60, 0xfe3c63, "start_png", 140, 180, "left");
                                self.addChildAt(start, 2);
                                start.$children[1].x = 20;
                                start.x = 230;
                                start.y = 603;
                        }
                }
        }
}